<?php
namespace App\Controllers\Home;
use App\Libs\Home\Home as libHome;
use App\Models\Home\Home;
use \App\Libs\Cache\Redis;
/**
*	领奖 手机号
*/
class Linjiang extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }
	
	public function execute(){
		$key = '';
		if (isset($_COOKIE[SESSION_KEY_OPEN]) && ! empty($_COOKIE[SESSION_KEY_OPEN])) {
            $key = trim($_COOKIE[SESSION_KEY_OPEN]);
		}
		
		$openid = \App\Libs\Cache\Redis::get(trim($key));
		if( empty( $openid ) ){
				return \App\Libs\Views\Json::view(1, '授权失败，请退出重新进入', null);	
		}
		$lid = (int)$this->request->get('lid');
		$tel = $this->request->get('phone');
		if( empty( $lid ) ){
			return \App\Libs\Views\Json::view(1, '页面错误，请刷新重试', null);	
		}
		if( empty( $tel ) ){
			return \App\Libs\Views\Json::view(1, '手机号不能为空', null);	
		}
		
		$choujiang = Home::find(array(
			'id = :lid: and openid = :openid:',
			'bind' => array('lid'=>$lid,'openid'=>$openid),
		));
		
		if( !empty( $choujiang ) ){
			//存在修改 
			$choujiang->utime = date("Y-m-d H:i:s");
			$choujiang->tel = $tel;
			$choujiang->is_get = 1;
			$choujiang->save();
			return \App\Libs\Views\Json::view(0, '领取成功', null);
		}else{
			return \App\Libs\Views\Json::view(1, '当前奖品不存在，请刷新重试！', null);
		}
		
	}
	
	
}
